﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DTcms.DBUtility;
using DTcms.Common;
using System.Collections.Generic;

namespace DTcms.DAL
{
    /// <summary>
    /// 数据访问类:ch_photos
    /// </summary>
    public partial class photos
    {
        public photos()
        { }
        #region 基本方法============================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ch_photos");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.photos model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 添加主表数据====================
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("insert into ch_photos(");
                        strSql.Append("title,keywords,description,img_url,caizhi_id,wenli_id,yongtu_id,yanse_id,province_id,city_id,district_id,content,status,add_time,company_id,user_id)");
                        strSql.Append(" values (");
                        strSql.Append("@title,@keywords,@description,@img_url,@caizhi_id,@wenli_id,@yongtu_id,@yanse_id,@province_id,@city_id,@district_id,@content,@status,@add_time,@company_id,@user_id)");
                        strSql.Append(";select @@IDENTITY");
                        SqlParameter[] parameters = {
					        new SqlParameter("@title", SqlDbType.NVarChar,100),
					        new SqlParameter("@keywords", SqlDbType.NVarChar,255),
					        new SqlParameter("@description", SqlDbType.NVarChar,255),
					        new SqlParameter("@img_url", SqlDbType.NVarChar,255),
					        new SqlParameter("@caizhi_id", SqlDbType.Int,4),
					        new SqlParameter("@wenli_id", SqlDbType.Int,4),
					        new SqlParameter("@yongtu_id", SqlDbType.Int,4),
					        new SqlParameter("@yanse_id", SqlDbType.Int,4),
					        new SqlParameter("@province_id", SqlDbType.Int,4),
					        new SqlParameter("@city_id", SqlDbType.Int,4),
					        new SqlParameter("@district_id", SqlDbType.Int,4),
					        new SqlParameter("@content", SqlDbType.NText),
					        new SqlParameter("@status", SqlDbType.TinyInt,1),
					        new SqlParameter("@add_time", SqlDbType.DateTime),
                            new SqlParameter("@company_id", SqlDbType.Int,4),
                            new SqlParameter("@user_id", SqlDbType.Int,4)};

                        parameters[0].Value = model.title;
                        parameters[1].Value = model.keywords;
                        parameters[2].Value = model.description;
                        parameters[3].Value = model.img_url;
                        parameters[4].Value = model.caizhi_id;
                        parameters[5].Value = model.wenli_id;
                        parameters[6].Value = model.yongtu_id;
                        parameters[7].Value = model.yanse_id;
                        parameters[8].Value = model.province_id;
                        parameters[9].Value = model.city_id;
                        parameters[10].Value = model.district_id;
                        parameters[11].Value = model.content;
                        parameters[12].Value = model.status;
                        parameters[13].Value = model.add_time;
                        parameters[14].Value = model.company_id;
                        parameters[15].Value = model.user_id;


                        object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters); //带事务
                        model.id = Convert.ToInt32(obj);//插入后赋值
                        #endregion
                        #region 添加图片相册====================
                        if (model.albums != null)
                        {
                            new DAL.photo_albums().Add(conn, trans, model.albums, model.id);
                        }
                        #endregion

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return 0;
                    }
                }
            }
            return model.id;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.photos model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 修改主表数据==========================
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update ch_photos set ");

                        strSql.Append("title = @title,");
                        strSql.Append("keywords = @keywords,");
                        strSql.Append("description = @description,");
                        strSql.Append("img_url = @img_url,");
                        strSql.Append("caizhi_id = @caizhi_id,");
                        strSql.Append("wenli_id = @wenli_id,");
                        strSql.Append("yongtu_id = @yongtu_id,");
                        strSql.Append("yanse_id = @yanse_id,");
                        strSql.Append("province_id = @province_id,");
                        strSql.Append("city_id = @city_id,");
                        strSql.Append("district_id = @district_id,");
                        strSql.Append("content = @content,");
                        strSql.Append("status = @status,");
                        strSql.Append("company_id = @company_id,");
                        strSql.Append("user_id = @user_id");
                        strSql.Append(" where id=@id");
                        SqlParameter[] parameters = {
			                new SqlParameter("@id", SqlDbType.Int,4) ,            
                            new SqlParameter("@title", SqlDbType.NVarChar,100) ,            
                            new SqlParameter("@keywords", SqlDbType.NVarChar,255) ,            
                            new SqlParameter("@description", SqlDbType.NVarChar,255) ,            
                            new SqlParameter("@img_url", SqlDbType.NVarChar,255) ,            
                            new SqlParameter("@caizhi_id", SqlDbType.Int,4) ,            
                            new SqlParameter("@wenli_id", SqlDbType.Int,4) ,            
                            new SqlParameter("@yongtu_id", SqlDbType.Int,4) ,            
                            new SqlParameter("@yanse_id", SqlDbType.Int,4) ,            
                            new SqlParameter("@province_id", SqlDbType.Int,4) ,            
                            new SqlParameter("@city_id", SqlDbType.Int,4) ,            
                            new SqlParameter("@district_id", SqlDbType.Int,4) ,            
                            new SqlParameter("@content", SqlDbType.NText) ,            
                            new SqlParameter("@status", SqlDbType.TinyInt,1),
                            new SqlParameter("@company_id", SqlDbType.Int,4),
                            new SqlParameter("@user_id", SqlDbType.Int,4)};

                        parameters[0].Value = model.id;
                        parameters[1].Value = model.title;
                        parameters[2].Value = model.keywords;
                        parameters[3].Value = model.description;
                        parameters[4].Value = model.img_url;
                        parameters[5].Value = model.caizhi_id;
                        parameters[6].Value = model.wenli_id;
                        parameters[7].Value = model.yongtu_id;
                        parameters[8].Value = model.yanse_id;
                        parameters[9].Value = model.province_id;
                        parameters[10].Value = model.city_id;
                        parameters[11].Value = model.district_id;
                        parameters[12].Value = model.content;
                        parameters[13].Value = model.status;
                        parameters[14].Value = model.company_id;
                        parameters[15].Value = model.user_id;

                        DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters);
                        #endregion

                        #region 修改图片相册==========================
                        //删除/添加/修改相册图片
                        new DAL.photo_albums().Update(conn, trans, model.albums, model.id);
                        #endregion

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int article_id)
        {
            //取得相册MODEL
            List<Model.photo_albums> albumsList = new DAL.photo_albums().GetList(article_id);

            //删除图片相册
            StringBuilder strSql2 = new StringBuilder();
            strSql2.Append("delete from ch_photo_albums");
            strSql2.Append(" where article_id=@article_id");
            SqlParameter[] parameters2 = {
					new SqlParameter("@article_id", SqlDbType.Int,4)};
            parameters2[0].Value = article_id;
            List<CommandInfo> sqllist = new List<CommandInfo>();
            CommandInfo cmd = new CommandInfo(strSql2.ToString(), parameters2);
            sqllist.Add(cmd);

            //删除主表
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ch_photos");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = article_id;
            cmd = new CommandInfo(strSql.ToString(), parameters);
            sqllist.Add(cmd);

            int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
            if (rowsAffected > 0)
            {
                new DAL.photo_albums().DeleteFile(albumsList); //删除图片
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.photos GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,title,keywords,description,img_url,caizhi_id,wenli_id,yongtu_id,yanse_id,province_id,city_id,district_id,content,status,add_time,company_id,user_id");
            strSql.Append(" from ch_photos");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Model.photos model = new Model.photos();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.photos GetModel(string title)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,title,keywords,description,img_url,caizhi_id,wenli_id,yongtu_id,yanse_id,province_id,city_id,district_id,content,status,add_time,company_id,user_id");
            strSql.Append(" from ch_photos");
            strSql.Append(" where title=@title");
            SqlParameter[] parameters = {
					new SqlParameter("@title", SqlDbType.NVarChar,100)};
            parameters[0].Value = title;

            Model.photos model = new Model.photos();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,title,keywords,description,img_url,caizhi_id,wenli_id,yongtu_id,yanse_id,province_id,city_id,district_id,content,status,add_time,company_id,user_id");
            strSql.Append(" FROM ch_photos ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM ch_photos");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        #endregion

        #region 扩展方法============================
        /// <summary>
        /// 返回标题名称
        /// </summary>
        public string GetTitle(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 title from ch_photos");
            strSql.Append(" where id=" + id);
            string title = Convert.ToString(DbHelperSQL.GetSingle(strSql.ToString()));
            if (string.IsNullOrEmpty(title))
            {
                return "-";
            }
            return title;
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public void UpdateField(int id, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ch_photos set " + strValue);
            strSql.Append(" where id=" + id);
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.photos DataRowToModel(DataRow row)
        {
            Model.photos model = new Model.photos();
            if (row != null)
            {
                #region 主表信息======================
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["title"] != null)
                {
                    model.title = row["title"].ToString();
                }
                if (row["keywords"] != null)
                {
                    model.keywords = row["keywords"].ToString();
                }
                if (row["description"] != null)
                {
                    model.description = row["description"].ToString();
                }
                if (row["img_url"] != null)
                {
                    model.img_url = row["img_url"].ToString();
                }
                if (row["caizhi_id"] != null && row["caizhi_id"].ToString() != "")
                {
                    model.caizhi_id = int.Parse(row["caizhi_id"].ToString());
                }
                if (row["wenli_id"] != null && row["wenli_id"].ToString() != "")
                {
                    model.wenli_id = int.Parse(row["wenli_id"].ToString());
                }
                if (row["yongtu_id"] != null && row["yongtu_id"].ToString() != "")
                {
                    model.yongtu_id = int.Parse(row["yongtu_id"].ToString());
                }
                if (row["yanse_id"] != null && row["yanse_id"].ToString() != "")
                {
                    model.yanse_id = int.Parse(row["yanse_id"].ToString());
                }
                if (row["province_id"] != null && row["province_id"].ToString() != "")
                {
                    model.province_id = int.Parse(row["province_id"].ToString());
                }
                if (row["city_id"] != null && row["city_id"].ToString() != "")
                {
                    model.city_id = int.Parse(row["city_id"].ToString());
                }
                if (row["district_id"] != null && row["district_id"].ToString() != "")
                {
                    model.district_id = int.Parse(row["district_id"].ToString());
                }
                if (row["content"] != null)
                {
                    model.content = row["content"].ToString();
                }
                if (row["status"] != null && row["status"].ToString() != "")
                {
                    model.status = int.Parse(row["status"].ToString());
                }
                if (row["add_time"] != null && row["add_time"].ToString() != "")
                {
                    model.add_time = DateTime.Parse(row["add_time"].ToString());
                }
                if (row["company_id"] != null && row["company_id"].ToString() != "")
                {
                    model.company_id = int.Parse(row["company_id"].ToString());
                }
                if (row["user_id"] != null && row["user_id"].ToString() != "")
                {
                    model.user_id = int.Parse(row["user_id"].ToString());
                }
                #endregion

                //相册信息
                model.albums = new DAL.photo_albums().GetList(model.id);
            }
            return model;
        }
        #endregion
    }
}
